package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.kh.KHUtil
import cn.com.kh.KHDBConfig
import org.apache.commons.lang3.StringUtils
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class apiSoftpContrl implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiSoftpContrl.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        String Syncpram = "";
        String Retupram = "";

        String getMethod = request.getMethod();
        if (getMethod.equals("GET")) {
            Syncpram = request.getQueryString();
            log.info("接收调用GET参数信息：" + Syncpram);
        } else if (getMethod.equals("POST")) {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                Syncpram += line;
            }

            log.info("接收调用POST报文信息：" + System.getProperty("line.separator") + Syncpram);
        }


        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            //业务逻辑部分
            String rolegroup_id = request.getParameter("rolegroup_id");
            String staff_id = request.getParameter("staff_id");

            // 处理主数据
            String dealSQL = '''Select R1.Auxlimit,
                                       R2.Busynum,
                                       Case
                                         When R1.Auxlimit Is Not Null And R1.Auxlimit != '' And R1.Auxlimit != 0 And R1.Auxlimit <= R2.Busynum Then
                                          -1
                                         Else
                                          1
                                       End As Iscanbusy
                                  From Rolegroup R1
                                  Left Join (Select Count(*) As Busynum
                                               From Kh_Agent_State_View R1
                                               Left Join Kh_Agent_Staff_View R2
                                                 On R1.Agent = R2.Agentid
                                               Left Join Staffrolegroup R3
                                                 On R2.Staff_Id = R3.Staff_Id
                                              Where R1.Type In ('login', 'busy')
                                                And R1.State = 1
                                                And R1.Busytype = 1
                                                And R3.Rolegroup_Id = ?) R2
                                    On 1 = 1
                                 Where R1.Rolegroup_Id = ?''';
            List<Map<String, Object>> selectCus = jt.queryForList(dealSQL, rolegroup_id, rolegroup_id);

            log.debug("selectCus：" + selectCus);
            log.debug("检查数据(" + rolegroup_id + ") 数量：" + selectCus.size());

            if (selectCus.size() == 1) {
                Retupram = selectCus.get(0).get("isCanBusy").toString();
                log.debug("示忙人数结果：" + Retupram);

                Retupram = KHUtil.isNull(Retupram) ? 1 : Retupram;
            } else {
                Retupram = 1;
            }

        } catch (Exception e) {
            log.error("数据处理错误", e);
            Retupram = "1"
        }

        log.debug("示忙控制返回结果：" + Retupram);
        return Retupram;
    }

}